import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'
import { User } from './entities/user.entity'
import { CreateUserDto } from './dto/create-user.dto'
import { BaseCrudService } from 'src/common/base-crud.service'
import { UpdateUserDto } from './dto/update-user.dto'

@Injectable()
export class UsersService extends BaseCrudService<User> {
  constructor(
    @InjectRepository(User)
    public repo: Repository<User>
  ) {
    super(repo)
  }

  async createOneSimple(dto: CreateUserDto) {
    const newUser = this.repo.create(dto)
    return await this.repo.save(newUser)
  }

  async updateOneSimple(id: string, dto: UpdateUserDto) {
    // TODO: 禁止用户修改某些字段
    const user = await this.repo.findOneOrFail(id)
    const toSave = this.repo.create({ ...user, ...dto })
    return await this.repo.save(toSave)
  }
}
